package userimage.texteditor;

import global.Utilities;

import java.util.TreeMap;

public class TextController {

    String text;
    int offset = 0;

    String[] text_lines;
    int linenum;

    TreeMap<String, FieldAttr> attrMap = new TreeMap<>();

    TextCore core;

    public TextController(TextCore tc) {
        this.core = tc;
    }

    public String getText() {
        return text;
    }

    public char getChar(int[] coord) {
        return getChar(coord[0], coord[1]);
    }

    public char getChar(int line, int row) {
        try{
            return text_lines[line].charAt(row - 1);
        }catch (IndexOutOfBoundsException exception) {
            return '\n';
        }
    }

    public void deleteChar(int[] coord) {
        deleteChar(coord[0], coord[1]);
    }

    public void deleteChar(int line, int row) {
        if (row != 0)
            text_lines[line] = text_lines[line].substring(0, row - 1) + text_lines[line].substring(row);
        else {
            if (line != 1) {
                text_lines[line - 1] += text_lines[line].substring(row);
                for (int i = line; i < linenum; i++) {
                    text_lines[i] = text_lines[i + 1];
                    for (int j = 0; j < core.editing.size(); j++) {
                        if (Utilities.isAfterer(new int[]{line, row}, core.editing.get(j))) {
                            core.editing.set(j, new int[]{core.editing.get(j)[0] - 1, core.editing.get(j)[1]});
                            core.start_choosing.set(j, new int[]{core.start_choosing.get(j)[0] - 1, core.editing.get(j)[1]});
                        }
                    }
                }
                linenum--;
                String[] tmp = new String[linenum + 1];
                System.arraycopy(text_lines, 0, tmp, 0, tmp.length);
                text_lines = tmp;
            }
        }
    }

    public void addChar(char c, int line, int row) {

    }

    public void setText(String text) {
        this.text = text;
        linenum = getLineNumRelatively();
        text_lines = new String[linenum + 1];
        text_lines[0] = "<line zero>";
        for (int i = 1; i < text_lines.length; i++) {
            text_lines[i] = getLineRelatively(i);
        }
    }

    public CRLF getCRLF() {
        for (int i = 0; i < text.length(); i++) {
            if (text.charAt(i) == '\n')
                return CRLF.LF;
            else if (text.charAt(i) == '\r')
                return CRLF.CR;
            else if (text.charAt(i) == '\r' && text.charAt(i + 1) == '\n')
                return CRLF.CRLF;
        }
        return null;
    }

    public int getLineLength(int line) throws IndexOutOfBoundsException {
        return text_lines[line].length();
    }

    public int getLineNum() {
        return linenum;
    }

    public int getLineNumRelatively() {
        int lin = 1;
        for (int i = 0; i < text.length(); i++) {
            switch (getCRLF()) {
                case LF:
                    if (text.charAt(i) == '\n')
                        lin++;
                case CR:
                    if (text.charAt(i) == '\r')
                        lin++;
                case CRLF:
                    if (text.charAt(i) == '\r' && text.charAt(i + 1) == '\n') {
                        lin++;
                        i++;
                    }
            }
        }
        return lin;
    }

    public String getLine(int line) {
        return text_lines[line];
    }

    private String getLineRelatively(int line) {
        int l = 1;
        int st;
        for (st = 0; l < line; st++) {
            if (text.charAt(st) == '\n')
                l++;
        }
        int end;
        try {
            //noinspection StatementWithEmptyBody
            for (end = st; text.charAt(end) != '\n'; end++) ;
        } catch (StringIndexOutOfBoundsException e) {
            return text.substring(st);
        }
        return text.substring(st, end);
    }
}
